package cc.fozone.note.service.impl;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cc.fozone.note.beans.Article;
import cc.fozone.note.db.controller.IArticleController;
import cc.fozone.note.help.CalendarHelper;
import cc.fozone.note.service.IArticleService;

@Service
public class ArticleServiceImpl implements IArticleService {
	@Resource IArticleController articleController;
	@Override
	public void insert(Article article) {
		// TODO Auto-generated method stub
		articleController.insertArticle(article);
	}
	
	@Override
	public Article selectLast() {
		// TODO Auto-generated method stub
		Article article = articleController.selectLast();
		return article;
	}

	@Override
	public void save(Article article) {
		// TODO Auto-generated method stub
		if(article == null) return ;
		long id = article.getId();
		Timestamp now = CalendarHelper.buildNow();
		if(id <= 0) {
			article.setPublishtime(now);
			article.setUpdatetime(now);
			articleController.insertArticle(article);
			return ;
		} 
		article.setUpdatetime(now);
		articleController.updateArticle(article);
	}
	
	@Override
	public List<Article> selectFuzzyByTitle(String title) {
		// TODO Auto-generated method stub
		return articleController.selectByTitle(title);
	}
	
	@Override
	public Article selectById(long id) {
		// TODO Auto-generated method stub
		return articleController.selectById(id);
	}
	
	@Override
	public void deleteById(long id) {
		// TODO Auto-generated method stub
		articleController.deleteById(id);
	}
}
